package com.aurora.oa.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.adapter.MainFragmentPageAdapter;
import com.aurora.oa.home.view.HomePageFragment;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.message.view.MessageFragment;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.mycenter.view.MyCenterFragment;
import com.aurora.oa.signin.view.SigninMainFragment;
import com.aurora.oa.signin.view.TrackActivity;
import com.aurora.oa.signin.view.TrackHistoryActivity;
import com.aurora.oa.work.view.WorkFragment;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/8 0008.
 */
public class MainActivity extends AppCompatActivity {

     Toolbar toolbar;
     TextView tv_title;
     TextView tv_right_title;
     List<Fragment> mfragments;
    private List<String> titles;
     ViewPager mviewpager;
     TabLayout tabs;
    private int[] tabIcons = {
            R.mipmap.ico_foot_home2x,
            R.mipmap.ico_foot_news2x,
            R.mipmap.ico_foot_work2x,
            R.mipmap.ico_foot_qd2x,
            R.mipmap.ico_foot_my2x,
    };

    private int[] tabIconsPressed ={
            R.mipmap.ico_foot_home_h2x,
            R.mipmap.ico_foot_news_h2x,
            R.mipmap.ico_foot_work_h2x,
            R.mipmap.ico_foot_qd_h2x,
            R.mipmap.ico_foot_my_h2x,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_aurora_main);
        init();
       // test();

    }
    private void test() {
        RequestHttpModel model = new RequestHttpModelImpl();
        model.requestHttpDataString(AuroraConstants.BASE_SERVER_URL + "/structure/listAndUsers", new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                String s =body;
            }

            @Override
            public void onRequestDataFail(String message) {

            }
        });

        model.requestHttpDataString(AuroraConstants.BASE_SERVER_URL + "/structure/listWithUsers", new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                String s =body;
            }

            @Override
            public void onRequestDataFail(String message) {

            }
        });
    }

    public void init() {
        initView();
        initValue();
        initEvent();
    }


    private void initView() {
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab =getSupportActionBar();
        //把原始标题置空，显示自己的居中标题
        ab.setTitle("");
        ab.setDisplayHomeAsUpEnabled(false);
        tv_title = (TextView)  findViewById(R.id.tv_title);
        tv_right_title =(TextView)  findViewById(R.id.tv_right_title);
        mviewpager = (ViewPager) findViewById(R.id.mviewpager);
        tabs = (TabLayout) findViewById(R.id.tabs);
    }
    private void initValue() {
        mfragments = new ArrayList<>();
        mfragments.add(HomePageFragment.newInstance("首页界面"));
        mfragments.add(MessageFragment.newInstance("消息界面"));
        mfragments.add(WorkFragment.newInstance("工作界面"));
        mfragments.add(SigninMainFragment.newInstance("签到界面"));
        mfragments.add(MyCenterFragment.newInstance("我的界面"));

        titles= new ArrayList<>();
        titles.add("首页");
        titles.add("消息");
        titles.add("工作");
        titles.add("签到");
        titles.add("我的");

        MainFragmentPageAdapter adapter = new MainFragmentPageAdapter(getSupportFragmentManager(),mfragments,titles);
        mviewpager.setAdapter(adapter);
        tabs.setupWithViewPager(mviewpager);
        tv_title.setText("互联网产业集群");
        //防止百度地图闪屏
        mviewpager.setCurrentItem(3);
        mviewpager.setCurrentItem(0);
        setupTabIcons();

    }

    private void setupTabIcons() {
        for(int i=0;i<titles.size();i++){
           TabLayout.Tab tab = tabs.getTabAt(i);
            tab.setCustomView(getTabView(i));
        }


    }

    private View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab,null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        ImageView img_title =(ImageView) view.findViewById(R.id.img_title);
        tv_title.setText(titles.get(position));
        img_title.setImageResource(tabIcons[position]);
        tv_title.setTextColor(getResources().getColor(R.color.tab_unselected_gray));
        view.setPadding(5,5,5,5);
        if(position==0){
            tv_title.setTextColor(getResources().getColor(R.color.tab_selected_blue));
            img_title.setImageResource(tabIconsPressed[position]);
        }
        return view;
    }


    private void initEvent() {

        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
              setTabSelected(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setTabUnselected(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void setTabSelected(TabLayout.Tab tab) {

       View view = tab.getCustomView();
        TextView tv = (TextView) view.findViewById(R.id.tv_title);
       // tv.setTextColor(Color.parseColor("#6f7171"));
        tv.setTextColor(getResources().getColor(R.color.tab_selected_blue));
        ImageView iv =(ImageView) view.findViewById(R.id.img_title);
        iv.setImageResource(tabIconsPressed[tab.getPosition()]);

        switch (tab.getPosition()){
            case 0:
                //标题设置为互联网产业集群
                tv_title.setText("互联网产业集群");
                tv_right_title.setVisibility(View.GONE);
            break;
            case 1:
                //标题设置消息
                tv_title.setText("消息");
                tv_right_title.setVisibility(View.GONE);
                break;
            case 2:
                //标题设置工作
                tv_title.setText("工作");
                tv_right_title.setVisibility(View.GONE);
                break;
            case 3:
                //标题设置为签到
                tv_title.setText("签到");
                tv_right_title.setVisibility(View.VISIBLE);
                tv_right_title.setText("足迹");
                tv_right_title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(MainActivity.this, TrackHistoryActivity.class));
                    }
                });
                break;
            case 4:
                //标题设置为我的
                tv_title.setText("我的");
                tv_right_title.setVisibility(View.GONE);
                break;
            default:
                break;
        }


    }

    private void setTabUnselected(TabLayout.Tab tab){
        View view = tab.getCustomView();
        TextView tv = (TextView) view.findViewById(R.id.tv_title);
        // tv.setTextColor(Color.parseColor("#6f7171"));
        tv.setTextColor(getResources().getColor(R.color.tab_unselected_gray));
        ImageView iv =(ImageView) view.findViewById(R.id.img_title);
        iv.setImageResource(tabIcons[tab.getPosition()]);
    }

}
